package com.RecipeBook.favourites;

import java.util.ArrayList;

import com.RecipeBook.browse.DataBaseHelper;
import com.RecipeBook.common.Recipe;
import com.RecipeBook.common.RecipeInProgress;

import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.cooking.CookingRecipe;
import com.RecipeBook.main.GlobalsManager;
import com.RecipeBook.main.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * List of favourite recipes. (And it is spelt perfectly correct, spellcheck. Get over it.)
 */
public class Favourites extends RecipeActivity {

	private ListView recipeList;
	private ArrayList<Recipe> recipes;
	private ArrayList<String> items;
	private DataBaseHelper dbHelper;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favourites);
		setTitle("My Favourites");

		// Retrieve from database
		recipes = new ArrayList<Recipe>();
		items = new ArrayList<String>();

		//Open up the database
		dbHelper = GlobalsManager.getGlobals().getDatabaseHelper();

		// Test recipes
		/*try {
			dbHelper.addToFavorites(10);
			dbHelper.addToFavorites(12);
			dbHelper.addToFavorites(20);
			dbHelper.addToFavorites(25);
			dbHelper.addToFavorites(15);
		}
		catch (Exception e) {
			Log.e("Favourites", "Adding favourites error: " + e.getMessage());
		}*/

		ArrayList<Integer> favs;
		try {
			favs = dbHelper.getFavourites();
		}
		catch (Exception e) {
			Log.e("Favourites", "Getting favourites error: " + e.getMessage());
			favs = new ArrayList<Integer>();
		}

		for (int i = 0; i < favs.size(); i++) {
			Recipe r = dbHelper.getRecipeFromId(favs.get(i));
			recipes.add(r);
			items.add(r.getName());
		}

		// Set ListView
		recipeList = (ListView)findViewById(R.id.FavouritesList);
		recipeList.setOnItemClickListener(itemClickListener);
		recipeList.setOnItemLongClickListener(longClickListener);

		if (favs.isEmpty())
		{
			recipeList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,new String[]{"You have no favourites. (Go and find a few!)"}));
		}
		else {
			//recipeList.setAdapter(new RIPAdapter(this, android.R.layout.simple_list_item_1, recipes));
			recipeList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));
		}

	}


	public void onDestroy ()
	{
		super.onDestroy();
		//dbHelper.close();
	}


	// LISTENERS -----------------------------------------------------

	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> av, View v, int position, long id) {
			/*Intent i = new Intent(v.getContext(), CookingRecipe.class);
			i.putExtra("index", position);
			startActivity(i);*/
		}
	};

	private OnItemLongClickListener longClickListener = new OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {

			final Recipe r = recipes.get(position);
			final int positionToRemove = position;
			AlertDialog.Builder adb=new AlertDialog.Builder(Favourites.this);
			adb.setMessage("Delete recipe from your favourites? :(");

			adb.setNegativeButton("Cancel", null);
			adb.setPositiveButton("Yes", new AlertDialog.OnClickListener() {
				@SuppressWarnings("unchecked")
				public void onClick(DialogInterface dialog, int which) {
					dbHelper.removeFavorite(r.getId());
					items.remove(positionToRemove);
					recipes.remove(positionToRemove);
					((ArrayAdapter<String>)(recipeList.getAdapter())).notifyDataSetChanged();
				}
			});
			adb.show();

			return true;
		}
	};



	// ADAPTERS -----------------------------------------------------

	/*private class RIPAdapter extends ArrayAdapter<Recipe> {

		private ArrayList<Recipe> recipeViews;

		public RIPAdapter(Context context, int textViewResourceId, ArrayList<Recipe> objects) {
			super(context, textViewResourceId, objects);
			recipeViews = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.rip_list_item, null);
			}

			Recipe r = recipeViews.get(position);
			if (r != null) {
				TextView name = (TextView) v.findViewById(R.id.TextView01);
				ImageView picture = (ImageView) v.findViewById(R.id.ImageView01);
				if (name != null) {
					name.setText(r.getName());
				}

				if(picture != null){
					//picture.setImageDrawable(Drawable.createFromPath(r.getImageFile()));
					try {
						Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(r.getImageFile()).getContent());
						picture.setImageBitmap(bitmap); 
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return v;
		}
	}*/
}


